package engine.input;

import util.Util;

public class Mouse 
{
    
    public static int // Mouse input indices
            CLICK_L, CLICK_R, CLICK_MID;
    
    
    private boolean[] mouse;
    private boolean[] mousePrev;
    
    public boolean // Mouse input bools
            WHEEL_UP, WHEEL_DOWN;
    
    private int mx;
    public int mx() 
    {
        return mx;
    }
    
    private int my;
    public int my()
    {
        return my;
    }
    
    private int dx;
    public int dx() 
    {
        return dx;
    }
    
    private int dy;
    public int dy() 
    {
        return dy;
    }
    
    private float mouseSens;
    public float mouseSens() 
    {
        return mouseSens;
    }
    
    private boolean mouseGrabbed;
    
    public Mouse(float mouseSensitivity) 
    {
        mouse = new boolean[3];
        mousePrev = new boolean[3];
        mouseGrabbed = false;
        mouseSens = mouseSensitivity;
        CLICK_L = 0;
        CLICK_R = 1;
        CLICK_MID = 2;
        org.lwjgl.input.Mouse.setGrabbed(mouseGrabbed);
        try 
        {
            org.lwjgl.input.Mouse.create();
        }
        catch (Exception e)
        {
            Util.console(e);
            Util.quit();
        }
        org.lwjgl.input.Mouse.setCursorPosition(100, 100);
    }
    
    public void update() 
    {
        dx = org.lwjgl.input.Mouse.getDX(); // Update mouse input vars
        dy = org.lwjgl.input.Mouse.getDY();
        mx = org.lwjgl.input.Mouse.getX();
        my = org.lwjgl.input.Mouse.getY();
        for (int i = 0; i < 3; i++) 
        {
            boolean isDown = org.lwjgl.input.Mouse.isButtonDown(i);
            mousePrev[i] = mouse[i];
            mouse[i] = isDown;
        }
        int dwheel = org.lwjgl.input.Mouse.getDWheel();
        WHEEL_UP = dwheel > 0;
        WHEEL_DOWN = dwheel < 0;
    }

    public void setCursorGrabbed(boolean grabbed) 
    {
        if (grabbed != mouseGrabbed) 
        {
            org.lwjgl.input.Mouse.setGrabbed(grabbed);
            mouseGrabbed = grabbed;
        }
    }

    public void setPos(int xpos, int ypos) 
    {
        org.lwjgl.input.Mouse.setCursorPosition(xpos, ypos);
    }
    
    public boolean isDown(int button) 
    {
        return mouse[button];
    }

    public boolean wasPressed(int button) 
    {
        return (mouse[button] && !mousePrev[button]);
    }

    public boolean wasReleased(int button) 
    {
        return (!mouse[button] && mousePrev[button]);
    }

    public void destroy() 
    {
        mouse = null;
        mousePrev = null;
        org.lwjgl.input.Mouse.destroy();
    }
    
}
